﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.BaseData
{
    public partial class EditClass : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPageUserType.Add("mana");
            strPagePrivilege.Add("BJSJ");

            tb.Style.Add("display", "none");
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] == null)
                {
                    Common.JShelper.JSAlert(this.Page, "", "系统出错！");
                }
                else
                {
                    BindDDl();
                    int ID =int.Parse(Request.QueryString["ID"].ToString());
                    InitBindFromDB(ID);
                }
            }
        }

        protected void InitBindFromDB(int ID)
        {
            LabMS.BLL.Class BC = new LabMS.BLL.Class();
            LabMS.Model.Class MC = new LabMS.Model.Class();
            MC = BC.GetModel(ID);
            tbClassName.Text = MC.Class_Name;
            tbClassNumber.Text = MC.Class_Code;//班号
            ddlClass.SelectedValue = MC.Class_Year;
            tbNum.Text = MC.Class_StudentNum.Value.ToString();
            tb.Text = MC.Class_Professional.Value.ToString();//专业代号
            ExtendBLL.Class EC = new ExtendBLL.Class();
            tbClassMajor.Text=EC.ProessionalNameByID(int.Parse(tb.Text.Trim()));
        }

        /// <summary>
        /// 年级绑定
        /// </summary>
        protected void BindDDl()
        {
            ExtendBLL.SysSetting ES = new ExtendBLL.SysSetting();
            int Year = ES.GetCurrentSetting().CurrentYear;
            int S = int.Parse(ES.GetCurrentSetting().CurrentSemester.Value.ToString());
            ddlClass.DataSource = Common.GradeHelper.GetCurrentGrade(Year, S, false);
            ddlClass.DataBind();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbClassNumber.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "班号不能为空！");
                tbClassNumber.Focus();
            }
            else if (string.IsNullOrEmpty(tbClassName.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "班级名称不能为空！");
                tbClassName.Focus();
            }
            else if (string.IsNullOrEmpty(tbNum.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "班级人数不能为空！");
                tbNum.Focus();
            }
            else if (string.IsNullOrEmpty(tbClassMajor.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "所属专业不能为空！");
                tbClassMajor.Focus();
            }
            else
            {
                LabMS.Model.Class cclass = new LabMS.Model.Class();
                ExtendBLL.Class extendclass = new ExtendBLL.Class();
                cclass.Class_Professional = int.Parse(tb.Text.Trim());//专业
                cclass.Class_Code = tbClassNumber.Text.Trim();//班级编号
                cclass.Class_Name = tbClassName.Text.Trim();//班级名称
                cclass.Class_Year = ddlClass.SelectedValue;//年级
                cclass.Class_StudentNum = int.Parse(tbNum.Text.Trim());//人数
                cclass.ID = int.Parse(Request.QueryString["ID"].ToString());
                try
                {
                    extendclass.Update(cclass);
                    Common.JShelper.JSAlertAndRedirect(this.Page, "", "更新成功！", "ClassData.aspx");
                }
                catch
                {
                    Common.JShelper.JSAlert(this.Page, "", "更新失败！");
                }
            }
        }
    }
}
